package com.yn.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yn.pet.dao.PetDao;
import com.yn.pet.entity.Pet;
import com.yn.pet.service.PetService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 宠物(人)信息表(Pet)表服务实现类
 *
 * @author makejava
 * @since 2025-07-09 12:00:48
 */
@Service("petService")
public class PetServiceImpl extends ServiceImpl<PetDao, Pet> implements PetService {

    @Override
    public List<Pet> listByDeviceIds(Set<Long> deviceIds) {
        Assert.notEmpty(deviceIds, "设备ID不能为空");
        return this.list(
                new LambdaQueryWrapper<Pet>()
                        .in(Pet::getDeviceId, deviceIds)
                        .eq(Pet::getDataStatus, 1)
                        .isNull(Pet::getUnBindTime)
        );
    }

    /**
     * 根据设备id查询宠物信息
     * @param deviceId 设备id
     * @return
     */
    @Override
    public List<Pet> getByDeviceId(Long deviceId) {
        if(deviceId == null){
            return null;
        }
        LambdaQueryWrapper<Pet> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(Pet::getDeviceId, deviceId)
                .isNull(Pet::getUnBindTime)
                .eq(Pet::getDataStatus, 1);
        return this.list(lambdaQuery);
    }

    /**
     * 根据设备id和用户id查询信息
     * @param deviceId 设备id
     * @param userId 用户id
     * @return
     */
    @Override
    public Pet getByDeviceIdAndUserId(Long deviceId, Long userId) {
        if(deviceId == null || userId == 0){
            return null;
        }
        LambdaQueryWrapper<Pet> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(Pet::getDeviceId, deviceId)
                .eq(Pet::getUserId, userId)
                .eq(Pet::getDataStatus, 1)
                .isNull(Pet::getUnBindTime);
        return this.getOne(lambdaQuery);
    }

    @Override
    public List<Pet> getByDeviceIdsAndUserId(Set<Long> deviceIds, Long userId) {
        if(CollectionUtils.isEmpty(deviceIds) || userId == 0){
            return Collections.emptyList();
        }
        LambdaQueryWrapper<Pet> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.in(Pet::getDeviceId, deviceIds)
                .eq(Pet::getUserId, userId)
                .eq(Pet::getDataStatus, 1)
                .isNull(Pet::getUnBindTime);
        return this.list(lambdaQuery);
    }


    /**
     * 根据userid获取用户绑定的设备信息列表
     * @param userId 用户id
     * @return List<Pet>
     */
    @Override
    public List<Pet> listByUserId(Long userId) {
        if(userId==null){
            return Collections.emptyList();
        }
        LambdaQueryWrapper<Pet> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(Pet::getUserId, userId)
                .isNull(Pet::getUnBindTime)
                .eq(Pet::getDataStatus, 1);
        return this.list(lambdaQuery);
    }
}

